#include "Connector.h"

CConnector::CConnector( boost::asio::io_service& io_service, const std::string& host, const std::string& service )
	: m_Connection( io_service )
{
	boost::asio::ip::tcp::resolver resolver( io_service );
	boost::asio::ip::tcp::resolver::query query( host, service );
		
	endpoint_iterator = resolver.resolve( query );
}

CConnector::~CConnector( void )
{
}

void CConnector::Connect( void )
{
	boost::asio::async_connect( m_Connection.GetSocket(), endpoint_iterator, boost::bind(&CConnector::OnConnect, this, boost::asio::placeholders::error) );
}

void CConnector::OnConnect( const boost::system::error_code& e )
{
	if( e )
	{
		OnConnectFailed( e );
		return;
	}

	OnConnectSuccessed();
}

void CConnector::OnRead( const boost::system::error_code& e, size_t bytes_transferred )
{
	if( e )
	{
		OnReadFailed( e, bytes_transferred );
		return;
	}

	OnReadSuccessed();
}

void CConnector::OnWrite( const boost::system::error_code& e )
{
	if( e )
	{
		OnWriteFailed( e );
		return;
	}

	OnWriteSuccessed();
}